package com.business_clean.ui.activity.address;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ZoomControls;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.business_clean.R;
import com.business_clean.app.base.BaseActivity;
import com.business_clean.data.mode.address.AddressListEntity;
import com.business_clean.databinding.ActivityPreviewAddressBinding;

import me.hgj.mvvmhelper.base.BaseViewModel;

public class PreviewAddressActivity extends BaseActivity<BaseViewModel, ActivityPreviewAddressBinding> {

    private BaiduMap mBaiduMap;

    private double lookLat, lookLnt;

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        mBaiduMap = mDatabind.map.getMap();
        if (getIntent() != null && getIntent().getExtras() != null) {
            AddressListEntity addressList = (AddressListEntity) getIntent().getExtras().getSerializable("address");
            if (addressList != null) {
                // 进行指定位置 定位
                lookLat = Double.parseDouble(addressList.getLat());
                lookLnt = Double.parseDouble(addressList.getLnt());
                mDatabind.llAddressInfo.setVisibility(View.VISIBLE);
                mDatabind.tvAddress.setText(addressList.getAddress());
                mDatabind.tvAddressDesc.setText(addressList.getAddress_desc());
                currentLocation(new LatLng(lookLat, lookLnt));
            }
        }
        initMapConfig();
    }

    private void initMapConfig() {
        //隐藏logo
        View child = mDatabind.map.getChildAt(1);
        if (child != null && (child instanceof ImageView || child instanceof ZoomControls)) {
            child.setVisibility(View.INVISIBLE);
        }
        // 不显示地图上比例尺
        mDatabind.map.showScaleControl(false);
        mDatabind.map.showZoomControls(false);

        //通过设置enable为true或false 选择是否禁用所有手势
        mBaiduMap.getUiSettings().setAllGesturesEnabled(false);
    }


    //定位到当前位置
    private void currentLocation(LatLng latLng) {
        MyLocationData locData = new MyLocationData.Builder()
                .accuracy(100)// 设置定位精度，单位：米
                .latitude(latLng.latitude)
                .longitude(latLng.longitude)
                .build();
        mBaiduMap.setMyLocationData(locData);


        MyLocationConfiguration config = new MyLocationConfiguration(MyLocationConfiguration.LocationMode.NORMAL, true, null);
        mBaiduMap.setMyLocationConfiguration(config);

        MapStatus.Builder builder = new MapStatus.Builder();
        builder.target(latLng).zoom(17.0f);// 调整缩放级别
        mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
    }

    @Override
    protected void onResume() {
        super.onResume();
        mDatabind.map.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mDatabind.map.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mDatabind.map.onDestroy();
        if (mBaiduMap != null) {
            mBaiduMap.clear();
            mBaiduMap = null;
        }
    }
}